// const origin = 'http://127.0.0.1:9999/api'
const origin = 'http://59.110.159.213:9999/api'

/// 上传文件
function uploadImg (avatarUrl){
    let tokenNow = uni.getStorageSync('token')
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: origin + "/upload",
            filePath: avatarUrl,
            name: 'file',
            header: {
              Authorization : 'bearer' + tokenNow
            },
            success (res){
				let data = JSON.parse(res.data)
				let url =''
                if(data.status == 200){
					url = data.data.fileUrl
                    resolve(url)
                }else{
                    resolve(false)
                }
            },
            fail: () => {
                console.log('上传失败了')
                resolve(false)
              }
        })

      })
}

export const postData = (options) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: origin + options.path,
			method: 'POST',
			data: options.data,
			timeout:3000,
			header: {
				'Authorization': uni.getStorageSync('token') || ''
			},
			success: (res) => {
				if (res.statusCode == 200) {
					resolve(res.data)
				} else {
					resolve(false)
				}
			},
			fail: (err) => {
				console.log('fail===',err)
				reject(err)
			}
		})
	})
}

export const getData = (options) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: origin + options.path,
			method: 'GET',
			header: {
				'Authorization': uni.getStorageSync('token') || ''
			},
			success: (res) => {
				if (res.statusCode == 200) {
					resolve(res.data)
				} else {
					resolve(false)
				}
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

export const putData = (options) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: origin + options.path,
			method: 'PUT',
			data: options.data,
			timeout:3000,
			header: {
				'Authorization': uni.getStorageSync('token') || ''
			},
			success: (res) => {
				if (res.statusCode == 200) {
					resolve(res.data)
				} else {
					resolve(false)
				}
			},
			fail: (err) => {
				console.log('fail===', options,err)
				reject(err)
			}
		})
	})
}

export const delData = (options) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: origin + options.path,
			method: 'DELETE',
			data: options.data,
			timeout:3000,
			header: {
				'Authorization': uni.getStorageSync('token') || ''
			},
			success: (res) => {
				if (res.statusCode == 200) {
					resolve(res.data)
				} else {
					resolve(false)
				}
			},
			fail: (err) => {
				console.log('fail===', options,err)
				reject(err)
			}
		})
	})
}
